//
//  AppRouter.swift
//  JYRouter
//
//  Created by huangguojian on 2021/9/16.
//

import UIKit
import JYRouter

/// Demo 路由
enum AppRouter: AppRouterType {
    
    case a(a0: String)
    case b(bbb: String?, c1: String?)
    case present
    case selectTabbar2
    case relogin
    case home
}

extension AppRouter: JYRouterProtocol {
    
    func tabbarIndex(of controller: JYRoutable) -> Int? {
        guard controller.routeStyle == .tabbarSelect else { return nil }
        return AppRouterManager.currentTabBarController.viewControllers?.firstIndex(of: controller)
    }
    
    var controller: JYRoutable? {
        switch self {
        case .a(a0: let a0):
            let vc = ViewController()
            vc.title = a0
            vc.view.backgroundColor = .white
            return vc
        case .present:
            let vc = PresentViewController()
            vc.view.backgroundColor = .green
            return vc
        case .b(bbb: let c0, c1: _):
            let vc = ViewController()
            vc.title = c0
            vc.view.backgroundColor = .white
            return vc
        case .selectTabbar2:
            return AppRouterManager.currentTabBarController.viewControllers?.filter({ $0.isKind(of: NavigationController<Index2Controller>.self) }).first as? JYRoutable
        case .relogin:
            return NavigationController(rootRoutable: ReloginController())
        case .home:
            return TabBarController()
        }
    }
    
    var willRemoveControllers: JYRouter.JYRouterWillRemoveControllersType {
        switch self {
        case .a,
                .present,
                .selectTabbar2,
                .relogin,
                .home:
            
            return .none
        case .b:
            return .include(vcTypes: [ViewController.self])
        }
    }
    
    
    var shouldLink: Bool {
        switch self {
        case .b:
            return false
        case .a,
                .present,
                .selectTabbar2,
                .relogin,
                .home:
            return true
        }
    }
    
    func handelAfterLinkFail() {
        switch self {
        case .b:
            AppRouterManager.main.relogin.link()
        case .a,
                .present,
                .selectTabbar2,
                .relogin,
                .home:
            break
        }
    }
    
    static func linkDidDecodeFail(_ link: String) {
        print("反序列化失败！link -> \(link)")
    }
}
